import { createFormActions, FormPath } from '@formily/antd';

export declare type linkTyp =
  | 'hide'
  | 'show'
  | 'visible'
  | 'props.enum'
  | 'loading'
  | 'loaded'
  | 'value'
  | 'disabled'
  | 'editable'
  | 'xProps'
  | 'required';
export declare interface Linkage {
  [propsName: string]: (key: linkTyp, defaultValue?: any) => void;
}
export const useLinkageUtils = () => {
  const { setFieldState } = createFormActions();
  const linkage = (key: linkTyp, defaultValue?: any) => (path: string, value?: any) =>
    setFieldState(path, state => {
      FormPath.setIn(state, key, value !== undefined ? value : defaultValue);
    });
  return {
    hide: linkage('visible', false),
    show: linkage('visible', true),
    visible: linkage('visible'),
    enum: linkage('props.enum', []),
    loading: linkage('loading', true),
    loaded: linkage('loading', false),
    value: linkage('value'),
    disabled: linkage('disabled'),
    editable: linkage('editable'),
    xProps: linkage('xProps'),
    required: linkage('required'),
  };
};
